package cn.ffcs.uec.staffAndOutStaff.component;

import java.io.ByteArrayOutputStream;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.zkoss.web.servlet.Servlets;
import org.zkoss.zk.ui.Components;
import org.zkoss.zk.ui.Executions;
import org.zkoss.zk.ui.IdSpace;
import org.zkoss.zk.ui.event.ForwardEvent;
import org.zkoss.zkplus.databind.BindingListModelList;
import org.zkoss.zul.Div;
import org.zkoss.zul.Filedownload;
import org.zkoss.zul.ListModel;
import org.zkoss.zul.Messagebox;

import cn.ffcs.uec.staffAndOutStaff.component.bean.OutStaffVocQuaListboxExtBean;
import cn.ffcs.uec.staffAndOutStaff.manager.OutStaffImportListManager;
import cn.ffcs.uec.staffAndOutStaff.vo.OutStaffImportListVo;
import cn.ffcs.uec.staffAndOutStaff.vo.OutStaffVocQua;
import cn.ffcs.uom.common.util.ApplicationContextUtil;
import cn.ffcs.uom.common.util.DateUtil;
import cn.ffcs.uom.common.util.ExportExcelNew;
import cn.ffcs.uom.common.util.IPortletInfoProvider;
import cn.ffcs.uom.common.util.ListboxUtils;
import cn.ffcs.uom.common.util.StrUtil;
import cn.ffcs.uom.common.vo.PageInfo;
import lombok.Getter;
import lombok.Setter;

/**
 * 外包人员导入模板清单--外包人员职业资格信息
 * 
 * @Title:
 * @Description:
 * @author Wang Yiren
 * @版权 FFCS（C） 2017
 * @date 2018年9月12日
 */
@Controller
@Scope("prototype")
public class OutStaffVocQuaListboxExt extends Div implements IdSpace {

    private static final long serialVersionUID = 6996871000359085823L;

    private static final String OUTSTAFF_VOCQUA_LIST_ZUL = "/pages_nmg/staffAndOutStaff/comp/outStaffVocQuaList_listbox_ext.zul";
    
    @Getter
    @Setter
    private OutStaffVocQuaListboxExtBean bean = new OutStaffVocQuaListboxExtBean();

    private OutStaffImportListManager outStaffImportListManager = (OutStaffImportListManager) ApplicationContextUtil
            .getBean("outStaffImportListManager");
    
    /**
     * 外包人员列表信息对象
     */
    private OutStaffImportListVo outStaffImportListVo = new OutStaffImportListVo();

    /**
     * 外包职业资格清单信息
     */
    private OutStaffVocQua outStaffVocQua = new OutStaffVocQua();
    
    /**
     * 下载数据
     */
    List<Map<String, Object>> data = new ArrayList<Map<String, Object>>();

    @Getter
    @Setter
    private IPortletInfoProvider portletInfoProvider;

    public OutStaffVocQuaListboxExt() {
        Executions.createComponents(OUTSTAFF_VOCQUA_LIST_ZUL, this, null);
        Components.wireVariables(this, bean);
        Components.addForwards(this, this, '$');
        
        this.addForward("onOrgTreeOutStaffQuery", this, "onOrgTreeOutStaffQueryResponse");
        
        onQuery();
    }

    /**
     * 界面初始化
     */
    public void onCreate() {
    }

    /**
     * 监听组织树抛出的事件
     * 
     * @param event
     */
    public void onOrgTreeOutStaffQueryResponse(final ForwardEvent event) {
        outStaffImportListVo = (OutStaffImportListVo) event.getOrigin().getData();
        if (null != outStaffImportListVo) {
            this.bean.getStaffCode().setValue(null);
            this.bean.getStaffName().setValue(null);
            outStaffVocQua.setPkOrg(outStaffImportListVo.getPkOrg());
            outStaffVocQua.setPkDept(outStaffImportListVo.getPkDept());
        }
        // 执行查询
        onQuery();
    }

    /**
     * 查询按钮
     */
    public void onQuery() {
        if (!StrUtil.isEmpty(this.bean.getStaffCode().getValue())) {
            outStaffVocQua.setPsnCode(this.bean.getStaffCode().getValue());
        }
        if (!StrUtil.isEmpty(this.bean.getStaffName().getValue())) {
            outStaffVocQua.setPsnName(this.bean.getStaffName().getValue());
        }
        onQueryOutStaffVocQua();
    }

    /**
     * 查询外包人员列表信息
     */
    public void onQueryOutStaffVocQua() {
      
        int activPage = bean.getOutStaffVocQuaListboxPaging().getActivePage();
        int pageSize = bean.getOutStaffVocQuaListboxPaging().getPageSize();
        PageInfo pageInfo = outStaffImportListManager.queryPageInfoByOutStaffVocQua(outStaffVocQua, activPage + 1,
                pageSize);
        // 清单数据
        data = outStaffImportListManager.getOutStaffVocQuaData(outStaffVocQua);
        // 总条数
        bean.getTotalSize().setValue(String.valueOf(pageInfo.getTotalCount()));
        ListModel list = new BindingListModelList(pageInfo.getDataList(), true);
        this.bean.getOutStaffVocQuaListboxs().setModel(list);
        this.bean.getOutStaffVocQuaListboxPaging().setTotalSize(pageInfo.getTotalCount());
    }

    /**
     * 每页显示事件
     */
    public void onPageSizeSelect() {
        if (this.bean.getPageListbox() != null && this.bean.getPageListbox().getSelectedItem().getValue() != null) {
            this.bean.getOutStaffVocQuaListboxPaging().setActivePage(0);
            this.bean.getOutStaffVocQuaListboxPaging()
                    .setPageSize(Integer.parseInt(this.bean.getPageListbox().getSelectedItem().getValue().toString()));
        }
        this.onQueryOutStaffVocQua();
    }

    /**
     * 翻页
     */
    public void onOutStaffVocQuaListboxPaging() {
        onQueryOutStaffVocQua();
    }

    /**
     * 清单下载按钮
     * 
     * @throws Exception
     */
    public void onDownloadlist() throws Exception {
        if (data.size() != 0) {
            // 设置字符集
            String charset = "UTF-8";
            String fileName = "外包人员职业资格信息_" + DateUtil.getYYYYMMDD(new Date());
            HttpServletRequest httpRequest = (HttpServletRequest) Executions.getCurrent().getNativeRequest();
            ByteArrayOutputStream os = new ByteArrayOutputStream();
            ExportExcelNew.exportExcel("", data, os);
            byte[] content = os.toByteArray();
            String encodedName = null;
            encodedName = URLEncoder.encode(fileName + ".xlsx", charset);
            // 将空格替换为+号
            encodedName = encodedName.replace("%20", "+");
            // 解决ie6 bug 或者是火狐浏览器
            if (encodedName.length() > 150 || Servlets.isGecko(httpRequest) || Servlets.isGecko3(httpRequest)) {
                encodedName = new String(encodedName.getBytes(charset), "ISO8859-1");
            }
            Filedownload.save(content, "application/octet-stream", encodedName);
        } else {
            Messagebox.show("数据为空!");
            return;
        }
    }

    /**
     * 重置按钮
     */
    public void onReset() {
        clearVariable();
        this.bean.getTotalSize().setValue(null);
        this.bean.getStaffName().setValue(null);
        this.bean.getStaffCode().setValue(null);
        ListboxUtils.clearListbox(this.bean.getOutStaffVocQuaListboxs());
        this.bean.getOutStaffVocQuaListboxPaging().setTotalSize(1);
        
        outStaffVocQua = new OutStaffVocQua();
    }

    /**
     * 清除变量
     */
    public void clearVariable() {
        outStaffVocQua = null;
        data.clear();
    }
}
